#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time   :2018/7/3 17:03
# @Author :huchao
# @E_mail :gmclqb@163.com
# @QQ : 九零九一三零零八三
# 功能描述： 视图函数

import traceback

import time
from django.http import HttpResponse,JsonResponse
from django.contrib.auth.hashers import make_password
from django.core.signing import Signer
import logging

from Fun import wx_small_login


def login_views(request):
    '''小程序用户登录方法'''
    if request.method == "POST":
        code = request.POST.get('code')
        wx = wx_small_login.WXLogin()
        session_key,openid = wx(code)
        print(session_key,openid)
        # 生成应用自己的sessionkey，并将其存到session系统中
        mySessionKey = session_key + openid + str(time.time())
        signer = Signer()
        mySessionKey = make_password(mySessionKey,
                                     signer.sign('shucheng'),
                                     'pbkdf2_sha1')
        request.session[mySessionKey] = {'sessionKey':session_key,
                                         'openid':openid}
        # 将自己生成的sessionkey，发送给client
        return JsonResponse({'sessionKey':mySessionKey})

    else:
        return JsonResponse({'errcode':-1})
